﻿var visitCreateVM = {
    PatientID: ko.observable(),
    PatientName: ko.observable(),
    Symptom: ko.observable(),
    Diagnosis: ko.observable(),
    Treatment: ko.observable(),
    loadVisit: function (indexController) {
        indexController.enableBtnCreateVisitSave(false);
        this.PatientID(patientVM_Doctor.Patient()[0].ID);
        this.PatientName(patientVM_Doctor.Patient()[0].Name);
        this.Symptom("");
        this.Diagnosis("");
        this.Treatment("");
        indexController.enableBtnCreateVisitSave(true);
    },
    saveVisit: function (indexController) {
        var self = this;
        indexController.enableBtnCreateVisitSave(false);
        indexController.enableBtnCreateVisitCancel(false);
        indexController.message.processing("saving");
        $.ajax({
            type: 'post',
            url: home + 'Doctor/CreateVisit',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: ko.toJSON(this),
            success: function (data, textStatus) {
                indexController.message.processComplete("save_success");
                indexController.displayCreateVisitForm(false);
                patientVM_Doctor.Visits.removeAll();
                patientVM_Doctor.Visits(data[0]);
                patientVM_Doctor.VisitsPage(data[1].PageNumber);
                patientVM_Doctor.VisitsPageCount(data[1].PageCount);
                indexController.enableVisitPreviousGroup(false);
                indexController.enableVisitNextGroup(false);
                patientVM_Doctor.VisitsPageNavHandling(indexController);
                //patientVM_Doctor.Visits.unshift(data[0]);
                //patientVM_Doctor.sortVisitsDESC;
            },
            error: function (err) {
                if (err.status == 200) {
                    indexController.message.processComplete(err.responseText);
                }
                else {
                    indexController.message.processComplete("save_fail");
                }
            },
            complete: function (xhr, textStatus) {
                indexController.enableBtnCreateVisitSave(true);
                indexController.enableBtnCreateVisitCancel(true);
            }
        });
    }
};